package com.zrrd.yunchmall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.sale.entity.Coupon;
import com.zrrd.yunchmall.sale.service.ICouponService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;


@RestController
@RequestMapping("/coupon")
@Api(tags = "优惠卷模块")
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @ApiOperation("查询优惠卷")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam int pageSize,@RequestParam int pageNum,String name){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        return new ResponseResult<>(200,"查询成功！",couponService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @ApiOperation("添加优惠卷")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Coupon coupon){
        couponService.save(coupon);
        return new ResponseResult<>(200,"查询成功！");
    }
    @ApiOperation("修改优惠卷")
    @PostMapping("/update/{id}")
    public ResponseResult updateById(@RequestBody Coupon coupon,@PathVariable("id") int id){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        couponService.update(coupon,updateWrapper);
        return new ResponseResult<>(200,"修改成功！",updateWrapper);
    }
    @ApiOperation("编辑优惠卷")
    @GetMapping("/{id}")
    public ResponseResult update(@PathVariable("id") int id){
        QueryWrapper queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return new ResponseResult<>(200,"修改成功!", couponService.getOne(queryWrapper));
    }
}
